<template>
  <q-layout>
    <q-layout-header ref="header">
      <xheader :title="title"></xheader>
    </q-layout-header>
    <q-page-container>
      <xscrollarea :scroll-height="scrollHeight">
        <q-page padding>
          <q-select
            v-model="userId"
            float-label="交班人"
            filter
            :options="userIdOptions" clearable
          />
          <q-select
            v-model="jcmember"
            float-label="交班成员"
            :options="jcmemberOptions" clearable
          />
          <q-select
            v-model="groupUserId"
            float-label="接班人"
            filter
            :options="groupUserIdOptions" clearable
          />
          <q-select
            v-model="memberid"
            float-label="接班成员"
            :options="memberidOptions" clearable
          />
          <div v-show="bzName!==''">
            <br>
            <q-card>
              <q-card-main>
                <q-item>
                  <q-item-main>
                    <div class="row">
                      <div class="col"><p>交班班次：</p></div>
                      <div class="col"><p>{{bzName}}</p></div>
                    </div>
                    <div class="row">
                      <div class="col"><p>接班班次：</p></div>
                      <div class="col"><p>{{bzName}}</p></div>
                    </div>
                    <div class="row">
                      <div class="col"><p>交接班时间：</p></div>
                      <div class="col"><p>{{dateTimes}}</p></div>
                    </div>
                  </q-item-main>
                </q-item>
                <q-card-separator></q-card-separator>
                <q-item>
                  <q-item-main>
                    <q-input float-label="工作内容" v-model="jobContent" type="textarea" rows="1"/>
                    <q-input float-label="接班注意事项" v-model="takeover" type="textarea" rows="1"/>
                    <q-input float-label="物品工具交接" v-model="goods" type="textarea" rows="1"/>
                  </q-item-main>
                </q-item>
              </q-card-main>

            </q-card>
            <br>
            <q-btn label="提交" color="primary" class="full-width" @click="submit"/>
          </div>
        </q-page>
      </xscrollarea>
    </q-page-container>
  </q-layout>
</template>

<script>

import utils from '../../js/utils'
import Xheader from '../../components/xheader'
import Xscrollarea from '../../components/xscrollarea'

export default {
  name: 'change_shifts',
  components: { Xscrollarea, Xheader },
  props: {
    title: {}
  },
  data: () => ({
    scrollHeight: 0,
    userId: '',
    userIdOptions: [],
    groupUserId: '',
    groupUserIdOptions: [],
    jcmember: '',
    jcmemberOptions: [],
    memberid: '',
    memberidOptions: [],
    takeover: '',
    bzName: '',
    goods: '',
    dateTimes: '',
    jobContent: '',
    orgId: '',
    groupId: '',
    jgroupId: ''
  }),
  computed: {},
  watch: {
    userId () {
      this.getJcmemberOptions()
    },
    groupUserId () {
      this.getMemberOptions()
    }
  },
  methods: {
    initUserIdData () {
      this.axios.post(this.$conf.server + '/ChangShifts/SelectUser', {}).then(res => {
        if (res.data.result) {
          for (let v of res.data.users) {
            this.userIdOptions.push({ value: v.ID, label: v.NAME })
            this.groupUserIdOptions.push({ value: v.ID, label: v.NAME })
          }
        }
      })
    },
    getJcmemberOptions () {
      this.data = []
      this.takeover = ''
      this.bzName = ''
      this.goods = ''
      this.dateTimes = ''
      this.jobContent = ''
      this.jcmemberOptions = []
      if (this.userId !== '') {
        this.axios.post(this.$conf.server + '/ChangShifts/SelectGroupuser',
          this.$qs.stringify({ user_id: this.userId })).then(res => {
          if (res.data.result && res.data.groups && res.data.groups.length) {
            for (let v of res.data.groups) {
              this.jcmemberOptions.push({ value: v.USERID, label: v.YHNAME })
            }
            this.bzName = res.data.groups[0].BZNAME
            this.dateTimes = res.data.groups[0].DATETIMES
            this.orgId = res.data.groups[0].ORGID
            this.groupId = res.data.groups[0].GROUPID
            let content = ''

            let companys = ''

            for (let i = 0; i < res.data.Maintain.length; i++) {
              if (i !== 0) {
                content += '         ' + (i + 1) + '、  对' + res.data.Maintain[i].SBNAME + '进行' +
                    res.data.Maintain[i].TYPES + ',保养项目为:' + res.data.Maintain[i].XMNAME
              } else {
                content += (i + 1) + '、  对' + res.data.Maintain[i].SBNAME + '进行' + res.data.Maintain[i].TYPES +
                    ',保养项目为:' + res.data.Maintain[i].XMNAME
              }
            }

            for (let i = 0; i < res.data.Repair.length; i++) {
              content += '         ' + (res.data.Maintain.length + 1 + i) + '、  对' + res.data.Repair[i].SBNAME + '进行' + res.data.Repair[i].TYPES
            }

            for (let i = 0; i < res.data.Runtime.length; i++) {
              content += '         ' + (res.data.Maintain.length + res.data.Repair.length + 1 + i) +
                  '、  对' + res.data.Runtime[i].SBNAME + '进行点检，点检项目为：' + res.data.Repair[i].XMNAME
            }

            for (let i = 0; i < res.data.Spareparts.length; i++) {
              if (res.data.Spareparts[i].COMPANYS === '10') {
                companys = '对'
              } else if (res.data.Spareparts[i].COMPANYS === '20') {
                companys = '个'
              } else if (res.data.Spareparts[i].COMPANYS === '30') {
                companys = '根'
              } else if (res.data.Spareparts[i].COMPANYS === '40') {
                companys = '盒'
              } else if (res.data.Spareparts[i].COMPANYS === '50') {
                companys = '卷'
              } else if (res.data.Spareparts[i].COMPANYS === '60') {
                companys = '块'
              } else if (res.data.Spareparts[i].COMPANYS === '70') {
                companys = '片'
              } else if (res.data.Spareparts[i].COMPANYS === '80') {
                companys = '套'
              } else if (res.data.Spareparts[i].COMPANYS === '90') {
                companys = '包'
              } else if (res.data.Spareparts[i].COMPANYS === '100') {
                companys = '箱'
              }
              content += '         ' + (res.data.Maintain.length + res.data.Repair.length + res.data.Runtime.length + 1 + i) +
                  '、  领用了' + res.data.Spareparts[i].NUMBERS + companys + res.data.Spareparts[i].WLNAME + '消耗到了' +
                  res.data.Spareparts[i].GXNAME + '工序的' + res.data.Spareparts[i].SBNAME + '设备上！'
            }
            this.jobContent = content
          }
        })
      }
    },
    getMemberOptions () {
      this.memberidOptions = []
      if (this.groupUserId !== '') {
        this.axios.post(this.$conf.server + '/ChangShifts/SelectGroupuser',
          this.$qs.stringify({ user_id: this.groupUserId })).then(res => {
          if (res.data.result && res.data.groups && res.data.groups.length) {
            this.jgroupId = res.data.groups[0].GROUPID
            for (let v of res.data.groups) {
              this.memberidOptions.push({ value: v.USERID, label: v.YHNAME })
            }
          }
        })
      }
    },
    submit () {
      let chestr = ''
      let remarks = ''
      for (let d of this.data) {
        if (d.checked) {
          chestr += d.XMCODE + ','
          remarks += utils.nvl(d.remark) + ','
        }
      }
      this.axios.post(this.$conf.server + '/ChangShifts/SaveSelectResult',
        this.$qs.stringify({
          lasrt_content: this.jobContent,
          Takeover: this.takeover,
          Goods: this.goods,
          outgroup: this.groupId,
          ingroup: this.jgroupId,
          userId: this.userId,
          jsuserId: this.groupUserId,
          jcmemberid: this.jcmemberid,
          jsmemberid: this.memberid,
          orgId: this.orgId
        }))
        .then(res => {
          if (res.data.results === 'false') {
            this.$parent.info('处理失败')
          } else {
            this.$parent.info('处理成功')
            this.userId = ''
            this.groupUserId = ''
            this.jcmember = ''
            this.jcmemberOptions = []
            this.memberid = ''
            this.memberidOptions = []
            this.takeover = ''
            this.bzName = ''
            this.goods = ''
            this.dateTimes = ''
            this.jobContent = ''
            this.orgId = ''
            this.groupId = ''
            this.jgroupId = ''
          }
        })
    }
  },
  mounted () {
    this.scrollHeight = this.$parent.webHeight - this.$refs.header.$el.clientHeight
  },
  created () {
    this.initUserIdData()
  }
}
</script>

<style scoped>

</style>
